package client.view;
import java.awt.BorderLayout;
import java.awt.List;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;

import client.remotes.ICommunicator;

import server.model.meeting.Message;
import client.whiteboard.*;
import client.whiteboard.model.DrawingData;
import client.whiteboard.model.ISubmitListener;
import client.whiteboard.model.Whiteboard;

public class MeetingGui extends UnicastRemoteObject implements ICommunicator {	
	
	private TextArea txtMessage; 
	private TextArea txtCompose;
	private JButton btnWhiteboard;
	private List participants;
	private JButton btnSend;
	private ClientMeeting clientMeeting;
	private String blaat;
	private JFrame frame;
	private String username;
	private Whiteboard _whiteboard;

	public MeetingGui(ClientMeeting clientMeeting, String username) throws RemoteException{
		this.username = username;
		this.clientMeeting = clientMeeting;
		frame = new JFrame();
		frame.setSize(400, 400);
		frame.setVisible(true);
		frame.setLayout(null);
		_whiteboard = null;
		initGUI();
	}
	
	private void initGUI() {
			
		txtMessage = new TextArea("voeg toe:");
		txtMessage.setEditable(false);
		txtMessage.setBounds(22, 12, 238, 249);
		
		txtCompose = new TextArea(); 
		txtCompose.setBounds(22, 273, 239, 77);		
		
		participants = new List();
		participants.setBounds(272, 12, 100, 120);		
		
		btnSend = new JButton("Send");
		btnSend.setBounds(273, 273, 100, 78);	
		
		btnSend.addActionListener(new btnSendListener());
		
		btnWhiteboard = new JButton();
		btnWhiteboard.setText("Whiteboard openen");
		btnWhiteboard.setBounds(272, 144, 109, 21);
		btnWhiteboard.addActionListener(new btnWhiteboardListener());
		
		frame.getContentPane().add(txtCompose);
		frame.getContentPane().add(txtMessage);
		frame.getContentPane().add(participants);		
		frame.getContentPane().add(btnSend);
		frame.getContentPane().add(btnWhiteboard);



	}

	
	private class btnSendListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			Date date = new Date();
			
			Message message = new Message(date, username , txtCompose.getText());

			txtCompose.setText("");
			
			clientMeeting.sendMessage(message);
			
		}
	}
	
	private class btnWhiteboardListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if(_whiteboard == null){
				_whiteboard = new Whiteboard(username, (ISubmitListener)clientMeeting);
			}else{
				_whiteboard.open();
				MsgBox message = new MsgBox(null, "Whiteboard is al open.");
			}
		}
	}
	
	

	public void SendMessage(Message msg) throws RemoteException {
		txtMessage.append(msg.getSender()+" zegt: "+msg.getContent()+"\n");
		
	}
	@Override
	public void SendWhiteboard(String username, DrawingData drawingData) throws RemoteException
	{
		_whiteboard.loadData(username, drawingData);
	}
	public String getName()
	{
		return username;
	}

	@Override
	public void updateParticipants(ArrayList<String> clients) throws RemoteException
	{
		participants.removeAll();
		for(int i = 0; i < clients.size(); i++){
			participants.add(clients.get(i));
		}
	}
}
